package com.xiaoyu.lanling.util;

import android.view.View;

public class ViewTagUtil {

    private final static int CP_VIEW_TAG = 30 << 24;

    public static <T> T getTag(View view, Class<T> cls) {
        if (view == null) {
            return null;
        }
        Object tag = view.getTag(CP_VIEW_TAG);
        if (tag == null || !(cls.isInstance(tag))) {
            return null;
        }
        return (T) tag;
    }

    public static void setViewTagForClick(View view, Object object) {
        if (view == null) {
            return;
        }
        view.setTag(CP_VIEW_TAG, object);
    }

    public static <T> T getTag(int key, View view, Class<T> cls) {
        key = (30 + key) << 24;
        if (view == null) {
            return null;
        }
        Object tag = view.getTag(key);
        if (tag == null || !(cls.isInstance(tag))) {
            return null;
        }
        return (T) tag;
    }

    public static void setViewTagForClick(int key, View view, Object object) {
        if (view == null) {
            return;
        }
        key = (30 + key) << 24;
        view.setTag(key, object);
    }


    public static void setViewTagForData(int key, View view, Object object) {
        if (view == null) {
            return;
        }
        key = (30 + key) << 24;
        view.setTag(key, object);
    }

    public static <T> T getViewTagForData(int key, View view, Class<T> cls) {
        Object tag = getViewTagForData(key, view);
        if (tag == null || !(cls.isInstance(tag))) {
            return null;
        }
        return (T) tag;
    }

    public static Object getViewTagForData(int key, View view) {
        if (view == null) {
            return null;
        }
        key = (30 + key) << 24;
        return view.getTag(key);
    }

}
